package com.gome.wechat.program.service.impl;

import com.gome.wechat.program.dao.GroupActivityUserRepo;
import com.gome.wechat.program.entity.GroupActivityUser;
import com.gome.wechat.program.entity.User;
import com.gome.wechat.program.service.GroupActivityUserService;

import java.util.ArrayList;
import java.util.List;

import com.gome.wechat.program.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * Created by liwei113 on 2017/11/2 0002.
 */
@Service
public class GroupActivityUserServiceImpl implements GroupActivityUserService {

    @Autowired
    GroupActivityUserRepo groupActivityUserRepo;

    @Autowired
    UserService userService;

    @Override
    public GroupActivityUser save(GroupActivityUser groupActivityUser) {
        return groupActivityUserRepo.save(groupActivityUser);
    }

	@Override
	public List<GroupActivityUser> getListByUserId(Long userId) {
		return groupActivityUserRepo.getListByUserId(userId);
	}

    @Override
    public List<User> findJoinUserPhotos(Long groupActivity) {
        List<GroupActivityUser> groupActivityUsers =  groupActivityUserRepo.findByGroupActivityId(groupActivity);
        if(CollectionUtils.isEmpty(groupActivityUsers)){
            return new ArrayList<>();
        }
        List<Long> ids = new ArrayList<>();
        for(GroupActivityUser tem:groupActivityUsers){
            ids.add(tem.getUserId());
        }
        return userService.findByUserIdIn(ids);
    }

    @Override
    public Long getJoinCount(Long groupActivityId) {
        return groupActivityUserRepo.countByGroupActivityId(groupActivityId);
    }

    @Override
    public GroupActivityUser findByUserAndActivityId(Long userId, Long activityId) {
        return groupActivityUserRepo.findByGroupActivityIdAndUserId(activityId,userId);
    }
}
